package com.gzlz.budejie.activity;

import java.util.List;

import com.google.gson.Gson;
import com.gzlz.budejie.R;
import com.gzlz.budejie.R.layout;
import com.gzlz.budejie.activity.SubscribeActivity.SubAdapter;
import com.gzlz.budejie.activity.SubscribeActivity.ViewHolder;
import com.gzlz.budejie.bean.SubscribeBean;
import com.gzlz.budejie.bean.SubscribeBean.RecommendData;
import com.gzlz.budejie.bean.SubscribeBean.SubscribedData;
import com.lidroid.xutils.BitmapUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SubscribeAddActivity extends Activity implements OnClickListener {

	private ImageButton iv_back_sub;
	private TextView tv_title_sub;
	private ImageButton ib_add_sub;
	private ImageView ib_search_sub;
	private ListView lv_sub;
	private RelativeLayout rl_sub;

	private String json;
	private BitmapUtils bitmapUtils;

	public List<RecommendData> rec_tags;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_subscribe);
		inintView();
		bitmapUtils = new BitmapUtils(SubscribeAddActivity.this);
		bitmapUtils.configDefaultBitmapConfig(Config.ARGB_4444);
		Intent intent = getIntent();
		json = intent.getStringExtra("json");
		Log.e("TAG", json.toString());
		processData(json);
	}

	private void inintView() {
		rl_sub = (RelativeLayout) findViewById(R.id.rl_sub);
		iv_back_sub = (ImageButton) findViewById(R.id.iv_back_sub);
		tv_title_sub = (TextView) findViewById(R.id.tv_title_sub);
		ib_add_sub = (ImageButton) findViewById(R.id.ib_add_sub);
		ib_search_sub = (ImageView) findViewById(R.id.ib_search_sub);
		lv_sub = (ListView) findViewById(R.id.lv_sub);
		ib_add_sub.setVisibility(View.GONE);

		iv_back_sub.setOnClickListener(this);
		ib_search_sub.setOnClickListener(this);
	}

	protected void processData(String json) {
		Gson gson = new Gson();
		SubscribeBean subscribedBean = gson.fromJson(json, SubscribeBean.class);
		rec_tags = subscribedBean.rec_tags;
		lv_sub.setAdapter(new SubAdapter());
	}

	class SubAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return rec_tags.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(SubscribeAddActivity.this,
						R.layout.item_sbuscribe, null);
				holder.iv_icon = (ImageView) convertView
						.findViewById(R.id.iv_sbu_icon);
				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_sbu_name);
				holder.tv_number = (TextView) convertView
						.findViewById(R.id.tv_sbu_nub);
				holder.btn_sbu = (Button) convertView
						.findViewById(R.id.btn_sbu);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
				RecommendData data = rec_tags.get(position);
				holder.tv_name.setText(data.theme_name);
				Log.e("TAG", "holder.tv_name");
				holder.tv_number.setText(data.sub_number + "人订阅");
				holder.btn_sbu.setText("+订阅");
				bitmapUtils.display(holder.iv_icon, data.image_list);
				return convertView;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back_sub:
			finish();
			break;
		case R.id.ib_search_sub:
			startActivity(new Intent(getApplicationContext(), SubscribeSearchActivity.class));
			break;
		default:
			break;

		}
	}
}
